package cn.duli.xianer.controller;

import cn.duli.xianer.pojo.MyResult;
import cn.duli.xianer.pojo.ReceiveAddress;
import cn.duli.xianer.service.ReceiveAddressService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/receiveAddress")
public class ReceiveAddressController {

    @Autowired
    private ReceiveAddressService receiveAddressService;


    /**
     * 新增收货地址
     * @param receiveAddress
     */
    @PostMapping("/save")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult save(ReceiveAddress receiveAddress) {
        receiveAddressService.save(receiveAddress);
        return new MyResult(200, "新增收货地址成功",null,null,null,null);
    }

    /**
     * 修改收货地址
     * @param receiveAddress
     */
    @RequestMapping("/update")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult update(ReceiveAddress receiveAddress) {
        receiveAddressService.update(receiveAddress);
        return new MyResult(200, "修改收货地址成功",null,null,null,null);
    }

    /**
     * 根据id删除收货地址（ra_status=0）
     *
     * @param raId
     */
    @RequestMapping("/deleteById/{raId}")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult deleteById(@PathVariable("raId") Integer raId) {
        receiveAddressService.deleteById(raId);
        return new MyResult(200, "根据id删除收货地址成功",null,null,null,null);
    }

    /**
     * 根据id查询此收货地址详情
     * @param raId
     * @return
     */
    @GetMapping("/findById/{raId}")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult findById(@PathVariable("raId") Integer raId){
        ReceiveAddress receiveAddress = receiveAddressService.findById(raId);
        return new MyResult(200, "根据id查询此收货地址详情成功",null,null,receiveAddress,null);
    }


    /**
     * 查询收货地址列表
     * @return
     */
    @GetMapping("/findAll")
    @RequiresRoles("admin")
    public MyResult findAll() {
        List<ReceiveAddress> list =  receiveAddressService.findAll();
        return new MyResult(200, "根据id查询此收货地址详情成功",null,list,null,null);
    }


    /**
     * 修改默认收货地址
     * @param receiveAddress
     */
    @RequestMapping("/updateDefault")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult updateDefault(ReceiveAddress receiveAddress) {
        receiveAddressService.updateDefault(receiveAddress);
        return new MyResult(200, "修改默认收货地址成功",null,null,null,null);
    }

    /**
     * 根据用户id查看该用户的收货地址列表
     * @param raUid
     * @return
     */
    @RequestMapping("/findByUserId/{raUid}")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult findByUserId(@PathVariable("raUid") Integer raUid) {
        List<ReceiveAddress> list = receiveAddressService.findByUserId(raUid);
        return new MyResult(200, "根据用户id查看该用户的收货地址列表成功",null,list,null,null);
    }

}
